package tmproject.hlhj.fhp.cartrans.fragments

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import kotlinx.android.synthetic.main.fgm_learn.view.*
import tmproject.hlhj.fhp.cartrans.BaseFgm
import tmproject.hlhj.fhp.cartrans.IView.HomeView
import tmproject.hlhj.fhp.cartrans.IView.LearnView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.activitys.ClassDetailAty
import tmproject.hlhj.fhp.cartrans.adapter.LearnListAdp
import tmproject.hlhj.fhp.cartrans.customVIews.ConfirmDialog
import tmproject.hlhj.fhp.cartrans.javaBean.ClassListBean
import tmproject.hlhj.fhp.cartrans.javaBean.DriverInfoBean
import tmproject.hlhj.fhp.cartrans.javaBean.LearInfoBean
import tmproject.hlhj.fhp.cartrans.javaBean.UnReadNumBean
import tmproject.hlhj.fhp.cartrans.presenter.HomePresenter
import tmproject.hlhj.fhp.cartrans.presenter.LearnPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper

/**
 * Created by Administrator on 2018/8/26/026.
 */
class LearnFragment :BaseFgm<LearnView,LearnPresenter>(), LearnView, HomeView, LearnListAdp.JumpListener {
    private lateinit var classListAdp:LearnListAdp
    private val datas=ArrayList<ClassListBean.DataBean.ListBean>()
    private var homePresenter: HomePresenter?=null
    private var page=1;
    private var hasMore=true
    private var pageSize=10;

    companion object {
        var alreadyTime=""
    }
    override fun bindView(): LearnView {
        return  this
    }

    override fun createPresenter(): LearnPresenter {
        homePresenter= HomePresenter()
        homePresenter?.createModule()
        homePresenter?.bindView(this)
        return LearnPresenter()
    }
    override fun getContentId(): Int {
        return  R.layout.fgm_learn
    }

    override fun initView() {
        classListAdp= LearnListAdp(datas)
        classListAdp.lisener=this
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=classListAdp

//        footNotingView?.visibility= View.GONE
        rootView.spView.header=DefaultHeader(context)

        classListAdp.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                if (hasMore){
                    page++
                    presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page,pageSize)
                }else{
                    classListAdp.loadMoreEnd()
                }

            }
        })

    }

    /**
     *司机信息
     */
    override fun setDriverInfo(driverInfoBean: DriverInfoBean) {
        rootView.driverName.text="${driverInfoBean?.data.name}(驾驶员)-${driverInfoBean?.data.tractor_no}"
    }

    /**
     * 无效
     */
    override fun setUnReadNum(unReadNumBean: UnReadNumBean) {

    }

    /**
     * 获取学习信息
     */
    override fun setLearn(learInfoBean: LearInfoBean) {
        if (learInfoBean.data.month_learned_time!=null){
            rootView.tvTime.text="${learInfoBean.data.month_remaining_time}分钟"
        }else{
            rootView.tvTime.text="${0.0}分钟"
        }
        if (learInfoBean.data.month_learned_time!=null){
            alreadyTime=learInfoBean.data.month_learned_time
        }else{
            alreadyTime="0.0"
        }

    }

    override fun setClassList(classListBean: ClassListBean?) {
        rootView.spView.onFinishFreshAndLoad()
        classListAdp.loadMoreComplete()
        if (null==classListBean){
            return
        }
        datas.addAll(classListBean.data.list)
        classListAdp.notifyDataSetChanged()

//        classListAdp.removeAllFooterView()
    }

    override fun logicStart() {
        presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page,pageSize)
        homePresenter?.getLearnInfo(context!!,SpHelper.getToken(context!!))
        homePresenter?.getInfo(context!!,SpHelper.getToken(context!!))
    }

    override fun resum() {
        super.resum()
        /*datas.clear()
        page=1
        presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page,pageSize)
        homePresenter?.getLearnInfo(context!!,SpHelper.getToken(context!!))
        homePresenter?.getInfo(context!!,SpHelper.getToken(context!!))*/
    }
    override fun setListener() {
        rootView.lo1.setOnClickListener {
        }

        rootView.spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
                page++
                presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page,pageSize)

            }
            override fun onRefresh() {
                hasMore=true
                classListAdp .setEnableLoadMore(true);
                datas.clear()
                page=1
                presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page,pageSize)

            }
        })
    }

    override fun jump(intent: Intent) {
        startActivityForResult(intent,1)
    }

    override fun noMoreData() {
        hasMore=false
        classListAdp.loadMoreEnd()
        rootView.spView.onFinishFreshAndLoad()
//        rootView.spView.footer=null
    }
}